package com.xyingsoft.zp.zP_Board.manager;

import org.bukkit.entity.Player;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class DataManager {
    private final Plugin plugin;
    private File dataFile;
    private FileConfiguration dataConfig;
    private FileConfiguration config;
    
    // 玩家数据映射表
    private final Map<UUID, PlayerData> playerDataMap = new HashMap<>();
    
    public DataManager(Plugin plugin) {
        this.plugin = plugin;
        this.config = plugin.getConfig();
        loadDataFile();
        loadAllPlayerData();
        
        // 定时保存数据
        int saveInterval = config.getInt("data.save_interval", 5);
        plugin.getServer().getScheduler().runTaskTimer(plugin, 
            this::saveAllData, saveInterval * 1200L, saveInterval * 1200L);
    }
    
    private void loadDataFile() {
        dataFile = new File(plugin.getDataFolder(), "playerdata.yml");
        if (!dataFile.exists()) {
            plugin.getDataFolder().mkdirs();
            try {
                dataFile.createNewFile();
            } catch (IOException e) {
                plugin.getLogger().severe("无法创建数据文件: " + e.getMessage());
            }
        }
        dataConfig = YamlConfiguration.loadConfiguration(dataFile);
    }
    
    private void loadAllPlayerData() {
        if (dataConfig.contains("players")) {
            for (String uuidStr : dataConfig.getConfigurationSection("players").getKeys(false)) {
                UUID uuid = UUID.fromString(uuidStr);
                String path = "players." + uuidStr + ".";
                
                int onlineTime = dataConfig.getInt(path + "onlineTime", 0);
                int blocksBreak = dataConfig.getInt(path + "blocksBreak", 0);
                int kills = dataConfig.getInt(path + "kills", 0);
                int deaths = dataConfig.getInt(path + "deaths", 0);
                int experience = dataConfig.getInt(path + "experience", 0);
                
                PlayerData data = new PlayerData(uuid);
                data.setOnlineTime(onlineTime);
                data.setBlocksBreak(blocksBreak);
                data.setKills(kills);
                data.setDeaths(deaths);
                data.setExperience(experience);
                
                playerDataMap.put(uuid, data);
            }
        }
    }
    
    public void saveAllData() {
        for (Map.Entry<UUID, PlayerData> entry : playerDataMap.entrySet()) {
            UUID uuid = entry.getKey();
            PlayerData data = entry.getValue();
            String path = "players." + uuid.toString() + ".";
            
            dataConfig.set(path + "onlineTime", data.getOnlineTime());
            dataConfig.set(path + "blocksBreak", data.getBlocksBreak());
            dataConfig.set(path + "kills", data.getKills());
            dataConfig.set(path + "deaths", data.getDeaths());
            dataConfig.set(path + "experience", data.getExperience());
        }
        
        try {
            dataConfig.save(dataFile);
        } catch (IOException e) {
            plugin.getLogger().severe("无法保存数据文件: " + e.getMessage());
        }
    }
    
    public PlayerData getPlayerData(Player player) {
        UUID uuid = player.getUniqueId();
        if (!playerDataMap.containsKey(uuid)) {
            PlayerData data = new PlayerData(uuid);
            playerDataMap.put(uuid, data);
        }
        return playerDataMap.get(uuid);
    }
    
    // 获取所有玩家数据
    public Map<UUID, PlayerData> getAllPlayerData() {
        return playerDataMap;
    }
    
    // 获取配置
    public FileConfiguration getConfig() {
        return config;
    }
} 